﻿using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using WorkFlowCore.Authorization;
using WorkFlowCore.Authorization.JWT;
using WorkFlowCore.Host.ViewModels.Account;
using WorkFlowCore.Host.ViewModels.Plugins;
using WorkFlowCore.Plugins;

namespace WorkFlowCore.Host.Controllers
{
    /// <summary>
    /// 插件管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class PluginController : AbpControllerBase
    {
        private readonly PluginManager pluginManager;
        private readonly IWorkflowSession workflowSession;

        public PluginController(PluginManager pluginManager, IWorkflowSession workflowSession)
        {
            this.pluginManager = pluginManager;
            this.workflowSession = workflowSession;
        }
        /// <summary>
        /// 获取插件应用配置列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetPluginApplyConfigs")]
        public async Task<PluginApplyConfigDto> GetPluginApplyConfigs()
        {
            if (!workflowSession.IsManager) return default;
            var configs =await pluginManager.GetPluginApplyConfigs();
            var manifests = pluginManager.GetPluginManifests();

            var result = PluginApplyConfigDto.FromPluginApplyConfig(configs.ToList(), pluginManager.GetPluginTypes(), manifests);

            return result;
        }
        /// <summary>
        /// 获取插件的描述信息
        /// </summary>
        /// <param name="executableTypeFullName">插件名</param>
        /// <returns></returns>
        [HttpGet("GetManifestWithConfigs")]
        public async Task<ManifestWithConfigDto[]> GetPluginManifests([FromQuery]string executableTypeFullName)
        {
            if (!workflowSession.IsManager) return default;
            var result = pluginManager.GetPluginManifests(executableTypeFullName).ToArray();
            return result.Adapt<ManifestWithConfigDto[]>();
        }
        /// <summary>
        /// 添加或者更新插件配置
        /// </summary>
        /// <param name="pluginApplyConfigDto"></param>
        /// <returns></returns>
        [HttpPost("AddOrUpdateConfig")]
        public async Task AddOrUpdateConfig([FromBody]PluginApplyConfigDto pluginApplyConfigDto)
        {
            if (!workflowSession.IsManager) return;
            var configs = pluginApplyConfigDto.ToPluginApplyConfig();
            await pluginManager.AddOrUpdateConfig(configs);
        }
        /// <summary>
        /// 载入插件（第一次使用需先载入）
        /// </summary>
        /// <returns></returns>
        [HttpGet("LoadPlugins")]
        public async Task LoadPlugins()
        {
            if (!workflowSession.IsManager) return;
            await pluginManager.AutoConfigureDefaultPlugins();
            
        }
        /// <summary>
        /// 获取身份认证插件
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetCustomizationVerifyExtensions")]
        [AllowAnonymous]
        public async Task<CustomizationVerifyExtensionDto[]> GetCustomizationVerifyExtensions()
        {
            var extensionInfos = await pluginManager.GetPluginApplyConfigs<ICustomizationVerifyExtension>();
            return CustomizationVerifyExtensionDto.FromPluginApplyConfig(extensionInfos.applyConfigs,extensionInfos.manifests);
        }
    }
}
